package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper,Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishVO
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishVO dishVO) {
        //保存菜品的基本信息到菜品表dish-----操作两个表dish、dish_flavor
        this.save(dishVO);

        Long dishId = dishVO.getId();  //菜品id

        //菜品口味
        List<DishFlavor> flavors = dishVO.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }
    /**
     * 修改菜品，同时保存对应的口味数据
     * @param dishVO
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishVO dishVO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        log.info("[>>>修改菜品--预添加dish]:{}",dish);

        //--1--  修改--覆盖菜品的数据
        this.updateById(dish);

        //--2--  删除--口味表关于菜品的数据
        dishFlavorService.remove(
                new LambdaQueryWrapper<DishFlavor>()
                        .eq(DishFlavor::getDishId,dish.getId())
        );

        //--3--  重新修改口味的 dish_id
        List<DishFlavor> flavors = dishVO.getFlavors();
        flavors=flavors.stream().map(item->{
            item.setDishId(dish.getId());
            return item;
        }).collect(Collectors.toList());

        //--4--  同事务下__修改第二个表[口味表]
        dishFlavorService.saveBatch(flavors);
    }


    //--5-- 起售/停售 批量起售/批量停售
    @Override
    public void restatusByIds(List<Long> ids, Integer num) {
        // update dish set status = ? where id in (1,2,3)
        LambdaUpdateWrapper<Dish> qw = new LambdaUpdateWrapper<>();
        qw.set(Dish::getStatus,num);// set status = ?
        qw.in(Dish::getId,ids);//  where id in (1,2,3)
        this.update(qw);

//        // update dish set status = ? where id = ?
//        for (Long id : ids) {
//            Dish dish = new Dish();
//            dish.setId(id);
//            dish.setStatus(num);
//            this.updateById(dish);
//        }
    }
}